package com.baozun.houji.order.viewmodel

import android.text.Editable
import androidx.lifecycle.MutableLiveData
import com.baozhun.mall.common.model.bean.GoodsInfoBean
import com.baozhun.mall.common.util.UploadFileHelper
import com.baozun.houji.order.network.orderApiService
import com.baozun.houji.order.R
import com.baozun.houji.order.model.bean.LogisticsCompanyInfoBean
import com.baozun.houji.order.model.bean.RefundInfoReqBean
import com.blankj.utilcode.util.ToastUtils
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.ext.requestNoCheck

/**
 * @Title：退货物流信息
 * @Description：
 * @Author：xing.luo
 * @Since：2022/11/20
 * @Version：1.0.0
 */
class RefundLogisticsInfoViewModel : BaseViewModel() {
    var mGoodsInfoBean = MutableLiveData<GoodsInfoBean>()
    var mLogisticsList = arrayListOf<LogisticsCompanyInfoBean>()
    var mLogisticsCompany = MutableLiveData<LogisticsCompanyInfoBean>()
    var mLogisticsNum = MutableLiveData<String>()
    var mRemark = MutableLiveData<String>()
    var mRemarkCount = MutableLiveData("（ 0/100 ）")

    /**
     * 获取物流公司信息
     */
    fun getLogisticsCompany() {
        request({ orderApiService.getLogisticsCompany() }, {
            mLogisticsList = it
        }, isShowDialog = false)
    }


    fun onTextChange(editable: Editable?) {
        mRemarkCount.value = "（ ${editable.toString().length}/100 ）"
    }

    /**
     * 提交申请
     */
    fun submit(afterSaleNo: String, files: MutableList<String>, successCallBack: (refundId: String) -> Unit) {
        checkParams {
            UploadFileHelper.getInstance().uploadMultiFile(this, files) { it ->
                var params = RefundInfoReqBean(
                    afterSaleNo,
                    mLogisticsNum.value,
                    mLogisticsCompany.value?.code,
                    mRemark.value,
                    it
                )
                requestNoCheck({ orderApiService.submitRefundInfo(params) }, {
                    if (it.isSuccess()) {
                        successCallBack.invoke(it.data)
                    }
                    ToastUtils.showShort(it.msg)
                }, isShowDialog = true)
            }
        }
    }

    private fun checkParams(checkSuccessCallBack: () -> Unit) {
        if (mLogisticsCompany.value == null) {
            ToastUtils.showShort(com.baozhun.mall.res.R.string.please_choose_logistics_company)
            return
        } else if (mLogisticsNum.value == null) {
            ToastUtils.showShort(com.baozhun.mall.res.R.string.please_choose_logistics_num)
            return
        }
        checkSuccessCallBack.invoke()
    }
}